<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Storage;
use DB;

class ZxLists extends Model
{
    protected $table = "kl_article";
    public $timestamps = false;

//是否热门
    const HOT_YES = 1;
    const HOT_NO = 0;
    static $HotDisplay = [
        self::HOT_NO => '非热门',
        self::HOT_YES => '热门',
    ];
	
//是否删除
    const DEL_YES = 1;
    const DEL_NO = 0;
    static $DelDisplay = [
        self::DEL_NO => '已删除',
        self::DEL_YES => '未删除',
    ];
	
//文章类型
    const TYPE_W = 1;
    const TYPE_V = 2;
    static $TypeDisplay = [
        self::TYPE_W => '文章',
        self::TYPE_V => '视频',
    ];
//发布状态
    const PUSH_YES = 0;
    const PUSH_NO = 1;
    static $PushDisplay = [
        self::PUSH_YES => '已发布',
        self::PUSH_NO => '未发布',
    ];

    /**
     * 新增
     * @param $request
     * @return array
     */
    static function zxartPublish($request)
    { 

        DB::beginTransaction();

        //添加资讯
        $hid = explode('||', $request->health_num_id);
        $advice = new ZxLists();
        $advice->title = $request->title;
        $advice->health_num_id = $hid[0];
        $advice->health_num_name = $hid[1];
		
        $advice->content = $request->contents;
        //$advice->hot = $request->hot;
        $advice->type = $request->type;
        $advice->column_id = $request->column_id;
       
		if($request->push_time){
			$advice->push_time = $request->push_time;
			$advice->create_time = $request->push_time;
			$advice->push_flag = 1;
			$advice->is_del = 0;
		}else{
			 $advice->create_time = date('Y-m-d H:i:s');
			 $advice->is_del = $request->is_del;
		}
		
		if($request->video){
			$filename = Postings::OssVideo($request->video);
			if ($filename['status'] !== 1) {

				return $filename;
			}
			
			$path = Storage::url($filename['message']);
			$advice->video = $path;
			
			if ($request->has('video_screenshots')) {
				$path = Storage::url(Postings::OssUpdate($request->video_screenshots));
				$advice->video_screenshots = $path;
			} else {
				return ['status' => 2, 'message' => '图片呢?'];
			}
			
			/*$video_path = Storage::disk('public')->put('/',$request->video);
			//截取视频第一帧
			$pic_path = storage_path('app/public/test.png');
			$str = "ffmpeg -i " . storage_path('app/public/' . $video_path) . " -y -f mjpeg -ss 0 -t 1 " . $pic_path;

			system($str);//调用命令行
			//将截图上传到oss
			$pic = fopen($pic_path, 'r');
			Storage::disk('oss')->put($filename['message'] . '.jpg', $pic);
			//删除视频和截图
			Storage::disk('public')->delete('test.png');
			Storage::disk('public')->delete($video_path);	
			$advice->video_screenshots = $request->video_screenshots;*/	
		}
        if (!$advice->save()) {
            DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }
        //添加封面图
        $insert = [];
        $i = 0;
        foreach ($request->pics as $key =>$value) {
			if($value){
				$insert[] = ['article_id' => $advice->id, 'pic_path' => $value,'create_time'=>date('Y-m-d H:i:s'),'health_num_id'=>$hid[0]];
			}
        }
		if(!empty($insert)){
			if (!DB::table('kl_article_pic')->insert($insert)) {
				DB::rollBack();

				return ['status' => 2, 'message' => '失败'];
			}
		}



        DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 修改
     * @param $advice
     * @param $request
     * @return array
     */
    static function zxartcirModify($advice, $request)
    {

       // DB::beginTransaction();

        //修改封面
		if(DB::table('kl_article_pic')->where('article_id', $advice->id)->first()){
			if (!DB::table('kl_article_pic')->where('article_id', $advice->id)->delete()) {
				//DB::rollBack();

				return ['status' => 2, 'message' => '失败'];
			}
		}

        //修改咨询
        $hid = explode('||', $request->health_num_id);
        $advice->title = $request->title;
        $advice->health_num_id = $hid[0];
        $advice->health_num_name = $hid[1];
		
        $advice->content = $request->contents;
        //$advice->hot = $request->hot;
        $advice->type = $request->type;
        $advice->is_del = $request->is_del;
        $advice->column_id = $request->column_id;
		$advice->update_time = date('Y-m-d H:i:s');
        $insert = [];
        $i = 0;
        foreach ($request->pics as $key =>$value) {
			if($value){
				$insert[] = ['article_id' => $advice->id, 'pic_path' => $value,'create_time'=>date('Y-m-d H:i:s'),'health_num_id'=>$hid[0]];
			}
        }
		if(!empty($insert)){
			if (!DB::table('kl_article_pic')->insert($insert)) {
				//DB::rollBack();

				return ['status' => 2, 'message' => '失败'];
			}		
		}
		if($request->video){
			$filename = Postings::OssVideo($request->video);
			if ($filename['status'] !== 1) {

				return $filename;
			}
			
			$path = Storage::url($filename['message']);
			
			$advice->video = $path;
			
			if($advice->video_screenshots==''&&$request->has('video_screenshots')==''){
				return ['status' => 2, 'message' => '请上传视频封面图'];
			}
			
			/*$video_path = Storage::disk('public')->put('/',$request->videos);
			//截取视频第一帧
			$pic_path = storage_path('app/public/test.png');
			$str = "ffmpeg -i " . storage_path('app/public/' . $video_path) . " -y -f mjpeg -ss 0 -t 1 " . $pic_path;

			system($str);//调用命令行
			//将截图上传到oss
			$pic = fopen($pic_path, 'r');
			Storage::disk('oss')->put($filename['message'] . '.jpg', $pic);
			//删除视频和截图
			Storage::disk('public')->delete('test.png');
			Storage::disk('public')->delete($video_path);	
			$advice->video_screenshots = $request->video_screenshots;*/	
		}
		

		if ($request->has('video_screenshots')) {
			$path = Storage::url(Postings::OssUpdate($request->video_screenshots));
			$advice->video_screenshots = $path;
		}

        if (!$advice->save()) {
            //DB::rollBack();

            return ['status' => 2, 'message' => '失败'];
        }

        //DB::commit();

        return ['status' => 1, 'message' => '成功'];
    }
	
    static function Del($id, $request)
    {
        DB::beginTransaction();

        if (!ZxLists::where('id', $id)->update([
            'is_del' => ZxLists::DEL_NO
        ])) {
            DB::rollBack();

            return ['status' => 2, 'message' => '删除失败'];
        }

        DB::commit();

        return ['status' => 1, 'message' => '删除成功'];
    }


}
